<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
            <el-form-item label="接口名称" prop="selectedOption">
                <el-select v-model="queryParams.name" placeholder="请选择接口名称" clearable style="width: 240px">
                    <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.name" />
                </el-select>
            </el-form-item>

            <el-form-item label="请求时间" style="width: 430px">
                <el-date-picker v-model="queryParams.startDate" type="date" placeholder="选择开始时间" style="width: 160px;"
                    value-format="YYYY-MM-DD" />
                <el-date-picker v-model="queryParams.endDate" type="date" clearable placeholder="选择截止时间"
                    style="width: 160px;" value-format="YYYY-MM-DD" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['system:role:export']">导出</el-button>
            </el-col>

        </el-row>

        <el-table ref="operlogRef" v-loading="loading" :data="list" @selection-change="handleSelectionChange"
            :default-sort="defaultSort" @sort-change="handleSortChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="日志编号" align="center" prop="id" width="200" />
            <el-table-column label="接口名称" align="center" prop="name" width="250" :show-overflow-tooltip="true" />
            <el-table-column label="主机" align="center" prop="ip" width="250" :show-overflow-tooltip="true" />
            <el-table-column label="请求参数" align="center" prop="param" width="400" :show-overflow-tooltip="true" />
            <el-table-column label="请求时间" align="center" prop="addtime" width="250" :show-overflow-tooltip="true">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.addtime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)"
                        v-hasPermi="['monitor:operlog:query']">详细</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 操作日志详细 -->
        <el-dialog title="操作日志详细" v-model="open" width="700px" append-to-body>
            <el-form :model="form" label-width="100px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="接口名称：">{{ form.name }}</el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="请求地址：">{{ form.ip }}</el-form-item>
                        <!-- <el-form-item label="请求方式：">{{ form.requestMethod }}</el-form-item> -->
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="请求参数：">{{ form.param }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="返回参数：">{{ form.result }}</el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="请求时间：">{{ parseTime(form.addtime) }}</el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="open = false">关 闭</el-button>
                </div>
            </template>
        </el-dialog>




        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </div>
</template>

<script setup name="Role">
import { ref, onMounted } from 'vue';
import { queryList, query } from "@/api/system/visit";

const { proxy } = getCurrentInstance();

const total = ref(0);
const showSearch = ref(true);;
const options = ref([]);
const list = ref([]);
const open = ref(false);

const data = reactive({
    list: ref([]),
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        startDate: "",
        endDate: ""
    },
});

const { queryParams, form, rules } = toRefs(data);


onMounted(async () => {
    await queryName();
    handleQuery();
});

async function queryName() {
    try {
        const response = await query();
        options.value = response;
    } catch (error) {
        console.error('遍历接口名称失败:', error);
    }
}
/** 搜索按钮操作 */
async function handleQuery() {
    const query = {
        startDate: data.queryParams.startDate,
        endDate: data.queryParams.endDate,
        name: data.queryParams.name,
        pageNum: data.queryParams.pageNum,
        pageSize: data.queryParams.pageSize
    };
    const response = await queryList(query);
    console.log(response)
    list.value = response.rows;
    total.value = response.total;
}
/** 重置按钮操作 */
function resetQuery() {
    data.queryParams.startDate = "";
    data.queryParams.endDate = "";
    data.queryParams.name = "";
    handleQuery();
}
/** 详细按钮操作 */
function handleView(row) {
    open.value = true;
    form.value = row;
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download("system/visit/export", {
        ...queryParams.value,
    }, `role_${new Date().getTime()}.xlsx`);
}
</script>
